package com.xing.adapter;

import java.util.List;

import com.xing.fragment.SurveyDetailFragment;
import com.xing.fragment.SurveyListSecondFragment;
import com.xing.model.HsdDataResearch;
import com.xing.questionnairesurvey.R;
import com.xing.util.DateUtil;
import com.xing.util.FragmentNameFlagList;
import com.xing.util.ViewUtil;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class SurveyListSecondAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private List<HsdDataResearch> mList;
	private Context mContext;
	private FragmentManager fragmentManager;
	
	public SurveyListSecondAdapter(Context context,List<HsdDataResearch> list,FragmentManager frmg){
		try {
			this.inflater = LayoutInflater.from(context);
			this.mList = list;
			this.mContext = context;
			this.fragmentManager = frmg;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("SurveyListSecondAdapter excep-------"+e.getMessage());
		}
		
	}
	

	public int getCount() {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.size();
		}
		return 0;
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		if(mList != null){
			return mList.get(arg0);
		}
		return null;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if(mList != null){
			return position;
		}
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final HsdDataResearch object  = mList.get(position);
			if(convertView == null){
				convertView = inflater.inflate(R.layout.survey_list_second_item, null);
			}
			
		
//		TextView nativeNumber = (TextView) convertView.findViewById(R.id.native_number);
		TextView nameTextView = (TextView) convertView.findViewById(R.id.name);
		TextView uploadTextView = (TextView) convertView.findViewById(R.id.update_time);
		TextView surveyStatusTextView = (TextView) convertView.findViewById(R.id.survey_status);
		Button surveyButton = (Button) convertView.findViewById(R.id.survey_button);
		Button scanButton = (Button) convertView.findViewById(R.id.scan);
		
//		nativeNumber.setText("本地编号:"+object.getResearchuuid());
		nameTextView.setText(String.valueOf(object.getName()));
		uploadTextView.setText(DateUtil.formatMyDate(String.valueOf(object.getModifytime())));
		/**
		 * 1 正要调查   2:已完成
		 */
		if (object.getStatus() == 1) {
			surveyStatusTextView.setText("正在调查");
		}else if(object.getStatus() == 2) {
			surveyStatusTextView.setText("完成");
		}
		
		
		surveyButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (object.getStatus() == 2) {
					ViewUtil.showToast(mContext,"问卷已经提交");
				}else {
					FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
					fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY_SECOND_FRAGMENT));
					SurveyDetailFragment surveyDetailFragment = new SurveyDetailFragment(); 
					Bundle bundle = new Bundle();
					bundle.putString("surveyuuid",object.getSurveyuuid());
					bundle.putString("researchuuid",object.getResearchuuid());
					bundle.putString("survey_name",SurveyListSecondFragment.surveyName);
					
					surveyDetailFragment.setArguments(bundle);
					fragmentTransaction.add(R.id.fragmentRoot, surveyDetailFragment, FragmentNameFlagList.SURVEY_DETAIL_FRAGMENT);
					fragmentTransaction.addToBackStack(FragmentNameFlagList.SURVEY_DETAIL_FRAGMENT);
					fragmentTransaction.commit();
				}
				
			}
		});
		
		scanButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
				fragmentTransaction.hide(fragmentManager.findFragmentByTag(FragmentNameFlagList.SURVEY_SECOND_FRAGMENT));
				SurveyDetailFragment surveyDetailFragment = new SurveyDetailFragment(); 
				Bundle bundle = new Bundle();
				bundle.putString("surveyuuid",object.getSurveyuuid());
				bundle.putString("researchuuid",object.getResearchuuid());
				bundle.putString("op_flag", "scan");
				
				surveyDetailFragment.setArguments(bundle);
				fragmentTransaction.add(R.id.fragmentRoot, surveyDetailFragment, FragmentNameFlagList.SURVEY_DETAIL_FRAGMENT);
				fragmentTransaction.addToBackStack(FragmentNameFlagList.SURVEY_DETAIL_FRAGMENT);
				fragmentTransaction.commit();
			}
		});
		return convertView;
	}
}
